package com.ld.security.server.admin.modules.gl.service.impl;


import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.lang.Console;
import com.ld.security.common.core.cache.TimeMap;
import com.ld.security.common.core.util.UuidUtil;
import com.ld.security.server.admin.modules.gl.config.UploadConfig;
import com.ld.security.server.admin.modules.gl.entity.GlFileEntity;
import com.ld.security.server.admin.modules.gl.po.SpiceFilePo;
import com.ld.security.server.admin.modules.gl.service.IGlFileService;
import com.ld.security.common.mybatis.base.service.ISuperServiceImpl;
import com.ld.security.server.admin.modules.gl.vo.SpiceFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

@Service
@Transactional
public class GlFileServiceImpl  extends ISuperServiceImpl<GlFileEntity>
implements
        IGlFileService {

        @Autowired
        UploadConfig uploadConfig;


        Map<String,SpiceFileVo> cache = new HashMap<>();


        @Autowired
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;


        long maxFileTime = 5 * 60;


        @Override
        public  String spliceFile(SpiceFilePo po) {
                String name = po.getMultipartFile().getOriginalFilename();
                String[] args = name.split("\\.");

                //不加锁查询
                SpiceFileVo vo =cache.get(po.getFileId());
                if(vo == null){
                        synchronized (cache){
                                vo =cache.get(po.getFileId());
                                if(vo == null){
                                       String fileName=args[1]+"/"+ UuidUtil.getId() +"."+args[1];
                                        File root  = new File(uploadConfig.getBasePath()+fileName);

                                        SpiceFileVo spiceFileVo = new SpiceFileVo(po.getFileNumber());
                                        spiceFileVo.setFileId(po.getFileId());
                                        spiceFileVo.setFileNumber(po.getFileNumber());
                                        spiceFileVo.setBasePath(uploadConfig.getTemPath()+File.separator+po.getFileId()+File.separator);
                                        spiceFileVo.setRoot(root);

                                        cache.put(po.getFileId(),spiceFileVo);

                                        //延迟5分钟后删除文件
                                        scheduledThreadPoolExecutor.schedule(() -> {


                                                        spiceFileVo.deleteAll();


                                        },maxFileTime,TimeUnit.SECONDS);

                                        vo = spiceFileVo;
                                }

                        }

                }

                String path=null;
                boolean result = vo.addFile(po);

                if(result){


                        path = uploadConfig.getWebPath() + args[1]+"/"+vo.getRoot().getName();
                }
                return path;
        }





        }
